package miao.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* miao.demo.dao.*Dao.find*(..))")
    private void pt(){}

    //@Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs(); //获取参数
        System.out.println(Arrays.toString(args));
        System.out.println("before advice...");
    }

    //@After("pt()")
    public void after(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println(Arrays.toString(args));
        System.out.println("after advice...");
    }

    //@Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        args[0] = 20;
        System.out.println("before advice...");
        Object ret = pjp.proceed(args);
        System.out.println("after advice...");
        return ret;
    }

    //@AfterReturning(value = "pt()",returning = "ret")
    public void afterReturning(JoinPoint jp,Object ret){ //两个参数位置不能乱
        Object[] args = jp.getArgs();
        System.out.println(Arrays.toString(args));
        System.out.println("return:" + ret);
        System.out.println("afterReturning advice...");
    }

    @AfterThrowing(value = "pt()",throwing = "t")
    public void afterTrowing(Throwable t){
        System.out.println("throwable: " + t);
        System.out.println("afterTrowing advice...");
    }
}
